package com.wyz.demo03;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyInvocationHandler implements InvocationHandler {
    //1.被代理的接口
    private Rent rent;
    public void setRent(Rent rent) {
        this.rent = rent;
    }
    //2.生成得到代理类
    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),
                rent.getClass().getInterfaces(),this);
    }

    @Override  //处理代理实列，并返回结果
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //动态代理的本质就是使用反射机制实现
        seeHouse();
        heTong();
        fare();
        Object o =  method.invoke(rent,args);
        return o;
    }

    public void seeHouse(){
        System.out.println("中介带我们看房子！");
    }
    public void heTong(){
        System.out.println("和中介签合同！");
    }
    public void fare(){
        System.out.println("收取中介费！");
    }
}
